package dse.modules;

import java.util.Map;
import java.util.Random;

import dse.DseException;
import dse.ExperimentConfiguration;
import dse.Network;
import dse.Node;

public class GeneralNodeBandwidthAssigner implements NodeBandwidthAssigner {
	private Random random;
	private double secondsPerPeriod;
	private int min;
	private int max;
	
	public void initalize (
			ExperimentConfiguration systemSettings,
			Map<String, String> moduleSettings
			)
	throws DseException
	{
		random = systemSettings.getRandom();
		secondsPerPeriod = systemSettings.getTimePeriodLength();
		
		ModuleUtil.SettingsReader sr = ModuleUtil.getSettingsReader(
				this.getClass(), moduleSettings
				);
		min = sr.getInteger("min");
		max = sr.getInteger("max");
	}
	
	public void assignBandwidths (Network network) {
		for (Node n : network.nodes())
			if (n.isSupernode())
				n.setBandwidthLimit(max);
			else
				n.setBandwidthLimit((int)
						((max - min)*random.nextDouble()*secondsPerPeriod + min)
						);
	}

}
